package com.cai.config;

import com.cai.filter.JwtAuthenticationTokenFilter;
import com.cai.handle.AuthenticationEntryPointImpl;
import com.cai.handle.LogoutSuccessHandlerImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * @Title: SecurityConfig
 * @Author RuoLi
 * @Package com.cai.config
 * @Date 2024/1/12 9:26
 * @description: 安全配置类
 */
@Slf4j
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private AuthenticationEntryPointImpl authenticationEntryPoint;
    @Autowired
    private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;
    @Autowired
    private LogoutSuccessHandlerImpl logoutSuccessHandlerImpl;
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Override
    public void configure(WebSecurity web) {
        //忽略路径
        web.ignoring().antMatchers(
                "/js/**",
                "/css/**",
                "/swagger-ui.html",
                "/doc.html",
                "/swagger-resources/**",
                "/v2/api-docs",
                "/webjars/**",
                "/swagger-resources/configuration/**");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //禁用iframe
        http.headers().frameOptions().sameOrigin();
        //禁用csrf
        http.csrf().disable();
        //跨域
        http.cors();
        //token过滤
        http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
        //禁用session
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
        //认证处理
        http.authorizeHttpRequests()
                .mvcMatchers("/login").permitAll()
                .anyRequest().authenticated();
        // 异常处理
        http.exceptionHandling().authenticationEntryPoint(authenticationEntryPoint);
        //退出登录
        http.logout()
                .logoutUrl("/logout")
                .permitAll()
                .deleteCookies("JSESSIONID")
                .logoutSuccessUrl("/doc.html")
                .logoutSuccessHandler(logoutSuccessHandlerImpl);
    }


}
